﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class OrderDAO
    {

        #region general

        private static Order LoadObj(IDataReader datareader)
        {
            Order obj = new Order();
            obj.OrderID = MpDataHelp.GetInt(datareader, "OrderID");
            obj.CustomerID = MpDataHelp.GetInt(datareader, "CustomerID");
            obj.ShippingMethodID = MpDataHelp.GetInt(datareader, "ShippingMethodID");
            obj.PaymentMethodID = MpDataHelp.GetInt(datareader, "PaymentMethodID");
            obj.FullName = MpDataHelp.GetString(datareader, "FullName");
            obj.Email = MpDataHelp.GetString(datareader, "Email");
            obj.Phone = MpDataHelp.GetString(datareader, "Phone");
            obj.Address = MpDataHelp.GetString(datareader, "Address");
            obj.Total = MpDataHelp.GetDouble(datareader, "Total");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");
            return obj;
        }

        #endregion

        public static int Order_Add(Order obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Order_Add");
            db.AddOutParameter(dbCommand, "OrderID", DbType.Int32, obj.OrderID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, obj.CustomerID);
            db.AddInParameter(dbCommand, "ShippingMethodID", DbType.Int32, obj.ShippingMethodID);
            db.AddInParameter(dbCommand, "PaymentMethodID", DbType.Int32, obj.PaymentMethodID);
            db.AddInParameter(dbCommand, "FullName", DbType.String, obj.FullName);
            db.AddInParameter(dbCommand, "Email", DbType.String, obj.Email);
            db.AddInParameter(dbCommand, "Phone", DbType.String, obj.Phone);
            db.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
            db.AddInParameter(dbCommand, "Total", DbType.String, obj.Total);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@OrderID"));
            return -1;
        }
        public static int Order_Update(Order obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Order_Update");
            db.AddInParameter(dbCommand, "OrderID", DbType.Int32, obj.OrderID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, obj.CustomerID);
            db.AddInParameter(dbCommand, "ShippingMethodID", DbType.Int32, obj.ShippingMethodID);
            db.AddInParameter(dbCommand, "PaymentMethodID", DbType.Int32, obj.PaymentMethodID);
            db.AddInParameter(dbCommand, "FullName", DbType.String, obj.FullName);
            db.AddInParameter(dbCommand, "Email", DbType.String, obj.Email);
            db.AddInParameter(dbCommand, "Phone", DbType.String, obj.Phone);
            db.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
            db.AddInParameter(dbCommand, "Total", DbType.String, obj.Total);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Order_Delete(int OrderID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Order_Delete");
            db.AddInParameter(dbCommand, "OrderID", DbType.Int32, OrderID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Order> Order_Select(int OrderID, int CustomerID, bool IsActive)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Order_Select");
            db.AddInParameter(dbCommand, "OrderID", DbType.Int32, OrderID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            List<Order> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Order>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
